#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include  <fcntl.h>    
#include <arpa/inet.h>
#include "pa_hx.h"
#include "crc-ccitt.h"
#include "pactrl.h"

#define set_mod_addr(msg, addr)  (*(mod_addr_t *)((uint8_t *)(msg) + HX_CMD_OFFSET_MOD_ADDR) = *(addr))
#define get_rsp_flag(msg)  (*((uint16_t *)(msg + HX_CMD_OFFSET_RSP_FLAG)))
#define set_cmd_code(msg, code)  (*((uint8_t *)(msg) + HX_CMD_OFFSET_CMD_CODE) = (code))


uint8_t hx_pa_powerswitch = 0;
uint8_t hx_lna_powerswitch = 0;
char hx_pa_sw_ver[11] = { 0 };


unsigned char AscToHex(unsigned char aHex)
{
    if((aHex>=0)&&(aHex<=9))
        aHex += 0x30;
    else if((aHex>=10)&&(aHex<=15))//A-F
        aHex += 0x37;
    else aHex = 0xff;
    return aHex;
}




int HexValue(unsigned char a)
{
	a -= 48;
	if(a > 9 ) a -= 7;
	return a;
}

int HexStrValue(char str[])
{
	int v=0;
	for(int ii=0 ;ii<(int)strlen(str); ii++)
	{ 
		v <<=4;
		v += HexValue(str[ii]);
	}
	return v;
}


inline uint32_t swap32(uint8_t *buf)
{
	uint32_t d = buf[0];
	d <<= 8;
	d |= buf[1];
	d <<= 8;
	d |= buf[2];
	d <<= 8;
	d |= buf[3];
	return d;
}

inline uint32_t swap16(uint8_t *buf)
{
	uint32_t d = buf[0];
	d <<= 8;
	d |= buf[1];
	return d;
}

inline uint32_t swap_u16(uint16_t buf)
{
	return (buf & 0xFF) << 8 | (buf & 0xFF00) >> 8;
}


inline pa_ret_t hx_verify_start_end_flag(void *buf, int32_t len)
{
	if( len >= 3 )
    {
        uint8_t *ptr = (uint8_t *)buf;
        if( (ptr[0] == HX_MSG_RSP_HEAD_FLAG) && ((((uint16_t)ptr[len - 1] == 0x0D))||(*(uint16_t *)((ptr+(len-2))) == 0x0A0D)))
        {
            return pa_ret_ok;
        }
    }

    PALogError("wrong start-end flag");
    return pa_err_magic;   
}

pa_ret_t hx_msg_check(uint8_t *buf, uint32_t len, mod_addr_t *lpa_addr)
{
    pa_ret_t ret = pa_ret_ok;

    ret = hx_verify_start_end_flag(buf, len);
    if( pa_ret_ok != ret )
    {
        return ret;
    }

    if( HX_RSP_FLAG_OK != get_rsp_flag(buf) )
    {       
        PALogError("check rsp flag failed, %d", get_rsp_flag(buf));
        return pa_err_rsp_flag;
    }

    return pa_ret_ok;
}

pa_ret_t hx_get_pa_temp(int32_t com_fd, mod_addr_t *pa_addr, hx_pa_status *status)
{
    pa_ret_t ret = pa_ret_ok;
    int32_t nret = 0;

    uint8_t com_cmd[13] = {0x3A, '1', '1', '0', '1', '0', '5', '0', '1', '0', '1', 0x0D, 0x0A};       

    set_mod_addr(com_cmd, pa_addr);
    nret = write(com_fd, com_cmd, 13);
    if( nret > 0 )
    {
        int32_t rsp_len = 0;
        uint8_t rsp_buf[256] = {0};
        usleep(500000);
        rsp_len = read(com_fd, rsp_buf, 256);

        if( rsp_len > 0 )
        {
            ret = hx_msg_check(rsp_buf, rsp_len, pa_addr);
            
            hx_pa_status read_statu = {0};
            
			char rsp_data[]={0};
			int value = 0;
            memcpy(rsp_data,rsp_buf+3, 2);     
            value = HexStrValue(rsp_data);

            read_statu.temp = value;            
            status->temp = read_statu.temp;

            if( pa_ret_ok != ret )
            {
                return ret;
            }
        }
        else
        {
            PALogError("nothing read");
            return pa_uart_read_zero;
        }
    }
    else
    {
        PALogError("error: can't write com port, ret=%d, errno=%d", nret, errno);
        return pa_err_uart_write;
    }
    return ret;

}

pa_ret_t hx_set_pa_switch(int32_t com_fd, mod_addr_t *pa_addr, int8_t on_off)
{
    pa_ret_t ret = pa_ret_ok;
    int32_t nret = 0;

    uint8_t com_cmd[13] = {0x3A, '1', '1', '0', '3', '0', '4', '0', '2', '0', '0', 0x0D, 0x0A};   

    set_mod_addr(com_cmd, pa_addr);
    if(0x01 == on_off)
    {
        com_cmd[HX_CMD_OFFSET_CMD_DATA] = 0x31;

    }
    else
    {
        com_cmd[HX_CMD_OFFSET_CMD_DATA] = 0x30;
    }

    nret = write(com_fd, com_cmd, 13);
	//PALogNotice("len is %d , write com_cmd is %s \n", nret, com_cmd);
	PALogInfo("len is %d , write com_cmd is %s \n", nret, com_cmd);
    if( nret > 0 )
    {

        int32_t rsp_len = 0;
        uint8_t rsp_buf[256] = {0};
        usleep(500000);
        rsp_len = read(com_fd, rsp_buf, 256);
		//PALogNotice("read buf is %s , len is %d \n", rsp_buf, rsp_len);
		PALogInfo("read buf is %s , len is %d \n", rsp_buf, rsp_len);
        if( rsp_len > 0 )
        {
            ret = hx_msg_check(rsp_buf, rsp_len, pa_addr);
            
            if( pa_ret_ok != ret )
            {
                return ret;
            }
        }
        else
        {
            PALogError("nothing read");
            return pa_uart_read_zero;
        }
    }
    else
    {
        PALogError("error: can't write com port, ret=%d, errno=%d",nret, errno);
        return pa_err_uart_write;
    }
    return ret;
}

pa_ret_t hx_set_up_att(int32_t com_fd, mod_addr_t *pa_addr, int8_t up_att)
{
    pa_ret_t ret = pa_ret_ok;

    int32_t nret = 0;

    uint8_t com_cmd[13] = {0x3A, '1', '1', '0', '3', 'F', 'A', '0', '0', '0', '0', 0x0D, 0x0A};  

    set_mod_addr(com_cmd, pa_addr);
    
    up_att = up_att/0.5;

    int data1, data2;
    data1 = AscToHex(((up_att&0xF0)/16)); 
    data2 = AscToHex(up_att&0x0F);
    com_cmd[HX_CMD_OFFSET_CMD_DATA-1] = data1;
    com_cmd[HX_CMD_OFFSET_CMD_DATA] = data2;
    
    nret = write(com_fd, com_cmd, 13);
    if( nret > 0 )
    {
        int32_t rsp_len = 0;
        uint8_t rsp_buf[256] = {0};
        usleep(500000);
        rsp_len = read(com_fd, rsp_buf, 256);
        if( rsp_len > 0 )
        {
            ret = hx_msg_check(rsp_buf, rsp_len, pa_addr);
            if( pa_ret_ok != ret )
            {
                return ret;
            }
 
        }
        else
        {
            PALogError("nothing read");
            return pa_uart_read_zero;
        }

    }
    else
    {
        PALogError("error: can't write com port, ret=%d, errno=%d", nret, errno);
        return pa_err_uart_write;
    }
    return ret;
}

pa_ret_t hx_get_up_att(int32_t com_fd, mod_addr_t *pa_addr, hx_pa_status *status)
{
    pa_ret_t ret = pa_ret_ok;
    int32_t nret = 0;

    uint8_t com_cmd[13] = {0x3A, '1', '1', '0', '1', 'F', 'A', '0', '0', '0', '1', 0x0D, 0x0A}; 
    
    set_mod_addr(com_cmd, pa_addr);
    nret = write(com_fd, com_cmd, 13);
    if( nret > 0 )
    {
        int32_t rsp_len = 0;
        uint8_t rsp_buf[256] = {0};
        usleep(500000);
        rsp_len = read(com_fd, rsp_buf, 256);
        if( rsp_len > 0 )
        {
            ret = hx_msg_check(rsp_buf, rsp_len, pa_addr);
            if( pa_ret_ok != ret )
            {

                return ret;
            }
            
            hx_pa_status read_statu = {0};
            
            char rsp_data[]={0};
			int value = 0;
            memcpy(rsp_data,rsp_buf+3, 2);          
            value = HexStrValue(rsp_data);
            read_statu.upatt = value;    
            status->upatt = read_statu.upatt;
      
        }
        else
        {
            PALogError("nothing read");
            return pa_uart_read_zero;
        }
    }
    else
    {
        PALogError("error: can't write com port, ret=%d, errno=%d",nret, errno);
        return pa_err_uart_write;
    }
    return ret;
}

pa_ret_t hx_set_dn_att(int32_t com_fd, mod_addr_t *pa_addr, int8_t dn_att)
{
    pa_ret_t ret = pa_ret_ok;
    int32_t nret = 0;
    uint8_t com_cmd[13] = {0x3A, '1', '1', '0', '3', 'F', 'A', '1', '1', '0', '0', 0x0D, 0x0A}; 
    
    set_mod_addr(com_cmd, pa_addr);
     
    dn_att = dn_att/0.5;
    int data1, data2;
    data1 = AscToHex(((dn_att&0xF0)/16)); 
    data2 = AscToHex(dn_att&0x0F);
     
    com_cmd[HX_CMD_OFFSET_CMD_DATA-1] = data1;
    com_cmd[HX_CMD_OFFSET_CMD_DATA] = data2;
    nret = write(com_fd, com_cmd, 13);
    if( nret > 0 )
    {
        int32_t rsp_len = 0;
        uint8_t rsp_buf[256] = {0};
        usleep(500000);
        rsp_len = read(com_fd, rsp_buf, 256);
        if( rsp_len > 0 )
        {
            ret = hx_msg_check(rsp_buf, rsp_len, pa_addr);
            if( pa_ret_ok != ret )
            {
                return ret;
            }
        }
        else
        {
            PALogError("nothing read");
            return pa_uart_read_zero;
        }
    }
    else
    {
        PALogError("error: can't write com port, ret=%d, errno=%d", nret, errno);
        return pa_err_uart_write;
    }
    return ret;
}

pa_ret_t hx_get_dn_att(int32_t com_fd, mod_addr_t *pa_addr, hx_pa_status *status)
{
    pa_ret_t ret = pa_ret_ok;
    int32_t nret = 0;

    uint8_t com_cmd[13] = {0x3A, '1', '1', '0', '1', 'F', 'A', '1', '1', '0', '1', 0x0D, 0x0A};  
    
    set_mod_addr(com_cmd, pa_addr);
    nret = write(com_fd, com_cmd, 13);
    if( nret > 0 )
    {
        int32_t rsp_len = 0;
        uint8_t rsp_buf[256] = {0};
        usleep(500000);
        rsp_len = read(com_fd, rsp_buf, 256);
        if( rsp_len > 0 )
        {
            ret = hx_msg_check(rsp_buf, rsp_len, pa_addr);
                        
            if( pa_ret_ok != ret )
            {
                return ret;
            }
           
            
            hx_pa_status read_statu = {0};
			char rsp_data[]={0};
			int value = 0;
            memcpy(rsp_data,rsp_buf+3, 2);          
            value = HexStrValue(rsp_data);
            read_statu.dnatt = value;
            status->dnatt = read_statu.dnatt;

        }
        else
        {
            PALogError("nothing read \n");
            return pa_uart_read_zero;
        }
    }
    else
    {

        PALogError("error: can't write com port, ret=%d, errno=%d", nret, errno);
        return pa_err_uart_write;
    }
    return ret;
}


pa_ret_t hx_get_dn_10_power(int32_t com_fd, mod_addr_t *pa_addr, hx_pa_status *status)
{
    pa_ret_t ret = pa_ret_ok;
    int32_t nret = 0;
    uint8_t com_cmd[13] = {0x3A, '1', '1', '0', '1', 'F', 'A', '2', '1', '0', '2', 0x0D, 0x0A};  
    

    set_mod_addr(com_cmd, pa_addr);
    nret = write(com_fd, com_cmd, 13);
    if( nret > 0 )
    {
        int32_t rsp_len = 0;
        uint8_t rsp_buf[256] = {0};
        usleep(500000);
        rsp_len = read(com_fd, rsp_buf, 256);
        if( rsp_len > 0 )
        {
            ret = hx_msg_check(rsp_buf, rsp_len, pa_addr);
            
            if( pa_ret_ok != ret )
            {
                return ret;
            }
                
            hx_pa_status read_statu = {0};
            char rsp_data[]={0};
            memcpy(rsp_data,rsp_buf+3, 4);

            int value, value2= 0;
            value = HexStrValue(rsp_data);   
            value2 = htons(value);
            read_statu.dn10power = value2;        
            status->dn10power = read_statu.dn10power;

        }
        else
        {
            PALogError("nothing read \n");
            return pa_uart_read_zero;
        }
    }
    else
    {
        PALogError("error: can't write com port, ret=%d, errno=%d",nret, errno);
        return pa_err_uart_write;
    }
    return ret;
}

pa_ret_t hx_get_pa_state(int32_t com_fd, mod_addr_t *pa_addr, hx_pa_status *status)
{
    pa_ret_t ret = pa_ret_ok;
    int32_t nret = 0;


    uint8_t com_cmd[13] = {0x3A, '1', '1', '0', '1', 'F', 'A', '3', '0', '0', '1', 0x0D, 0x0A};  
    set_mod_addr(com_cmd, pa_addr);

    nret = write(com_fd, com_cmd, 13);
    if( nret > 0 )
    {

        int32_t rsp_len = 0;
        uint8_t rsp_buf[256] = {0};
        usleep(500000);
        rsp_len = read(com_fd, rsp_buf, 256);
        if( rsp_len > 0 )
        {
            ret = hx_msg_check(rsp_buf, rsp_len, pa_addr);
             
            if( pa_ret_ok != ret )
            {
                return ret;
            }
            
            hx_pa_status read_statu = {0};
            char rsp_data[]={0};
			int value = 0;
            memcpy(rsp_data,rsp_buf+3, 2);      
            value = HexStrValue(rsp_data);
            read_statu.state = value;          
            status->state = read_statu.state;

        }
        else
        {
            PALogError("nothing read \n");
            return pa_uart_read_zero;
        }
    }
    else
    {
        PALogError("error: can't write com port, ret=%d, errno=%d",nret, errno);
        return pa_err_uart_write;
    }
    return ret;
}

pa_ret_t hx_get_pa_10_swr(int32_t com_fd, mod_addr_t *pa_addr, hx_pa_status *status)
{
    pa_ret_t ret = pa_ret_ok;
    int32_t nret = 0;

    uint8_t com_cmd[13] = {0x3A, '1', '1', '0', '1', 'F', 'A', '4', '0', '0', '1', 0x0D, 0x0A};  

    set_mod_addr(com_cmd, pa_addr);

    nret = write(com_fd, com_cmd, 13);
    if( nret > 0 )
    {
        int32_t rsp_len = 0;
        uint8_t rsp_buf[256] = {0};
        usleep(500000);
        rsp_len = read(com_fd, rsp_buf, 256);
        if( rsp_len > 0 )
        {
            ret = hx_msg_check(rsp_buf, rsp_len, pa_addr);

        
            if( pa_ret_ok != ret )
            {
                return ret;
            }
            
            hx_pa_status read_statu = {0};
            char rsp_data[]={0};			
            int value = 0;
            memcpy(rsp_data,rsp_buf+3, 2);
            value = HexStrValue(rsp_data);
     
            read_statu.sw10ratio = value;            
            status->sw10ratio = read_statu.sw10ratio;

        }
        else
        {
            PALogError("nothing read");
            return pa_uart_read_zero;
        }
    }
    else
    {
        PALogError("error: can't write com port, ret=%d, errno=%d",nret, errno);
        return pa_err_uart_write;
    }
    return ret;
}

pa_ret_t hx_set_mod_reset(int32_t com_fd, mod_addr_t *pa_addr)
{
    pa_ret_t ret = pa_ret_ok;
    int32_t nret = 0;

    uint8_t com_cmd[13] = {0x3A, '1', '1', '0', '3', 'F', 'A', 'A', '0', '0', '1', 0x0D, 0x0A};  
    
    set_mod_addr(com_cmd, pa_addr);
    nret = write(com_fd, com_cmd, 13);
    if( nret > 0 )
    {
        int32_t rsp_len = 0;
        uint8_t rsp_buf[256] = {0};
        usleep(500000);
        rsp_len = read(com_fd, rsp_buf, 256);
        if( rsp_len > 0 )
        {
            ret = hx_msg_check(rsp_buf, rsp_len, pa_addr);
            
            if( pa_ret_ok != ret )
            {
                return ret;
            }
        }
        else
        {
            PALogError("nothing read");
            return pa_uart_read_zero;
        }
    }
    else
    {
        PALogError("error: can't write com port, ret=%d, errno=%d",nret, errno);
        return pa_err_uart_write;
    }
    return ret;
}


pa_ret_t hx_get_dn_power(int32_t com_fd, mod_addr_t *pa_addr, hx_pa_status *status)
{
	pa_ret_t ret = pa_ret_ok;
    int32_t nret = 0;

    uint8_t com_cmd[13] = {0x3A, '1', '1', '0', '1', 'F', 'A', '2', '0', '0', '1', 0x0D, 0x0A};  
    
    set_mod_addr(com_cmd, pa_addr);
    nret = write(com_fd, com_cmd, 13);
    
    if( nret > 0 )
    {
        int32_t rsp_len = 0;
        uint8_t rsp_buf[256] = {0};
        usleep(500000);
        rsp_len = read(com_fd, rsp_buf, 256);
        if( rsp_len > 0 )
        {
            ret = hx_msg_check(rsp_buf, rsp_len, pa_addr);
           
            if( pa_ret_ok != ret )
            {
                return ret;
            }
            
            hx_pa_status read_statu = {0};
            char rsp_data[]={0};		
            int value = 0;
            memcpy(rsp_data,rsp_buf+3, 2);
            value = HexStrValue(rsp_data);
            read_statu.dnpower = value;          
            status->dnpower = read_statu.dnpower;
        
        }
        else
        {
            PALogError("nothing read");
            return pa_uart_read_zero;
        }
    }
    else
    {
        PALogError("error: can't write com port, ret=%d, errno=%d", nret, errno);
        return pa_err_uart_write;
    }
    return ret;
}


pa_ret_t hx_get_pa_switch(int32_t com_fd, mod_addr_t *pa_addr, hx_pa_status *status)
{
    pa_ret_t ret = pa_ret_ok;
    int32_t nret = 0;

    uint8_t com_cmd[13] = {0x3A, '1', '1', '0', '1', '0', '4', '0', '2', '0', '1', 0x0D, 0x0A};
    
    set_mod_addr(com_cmd, pa_addr);
    nret = write(com_fd, com_cmd, 13);
    if( nret > 0 )
    {
        int32_t rsp_len = 0;
        uint8_t rsp_buf[256] = {0};
        usleep(500000);
        rsp_len = read(com_fd, rsp_buf, 256);
        
        if( rsp_len > 0 )
        {
            ret = hx_msg_check(rsp_buf, rsp_len, pa_addr);

            if( pa_ret_ok != ret )
            {
                return ret;
            }

        	hx_pa_status read_statu = {0};
			char rsp_data[]={0};		
            int value = 0;
            memcpy(rsp_data,rsp_buf+3, 2);
            value = HexStrValue(rsp_data);            
            read_statu.on_off = value;
            status->on_off = read_statu.on_off;
            
        }
        else
        {
            PALogError("nothing read");
            return pa_uart_read_zero;
        }
    }
    else
    {
        PALogError("error: can't write com port, ret=%d, errno=%d", nret, errno);
        return pa_err_uart_write;
    }
    return ret;

}

pa_ret_t hx_get_pa_temp_threshold(int32_t com_fd, mod_addr_t *pa_addr, hx_pa_status *status)
{
    pa_ret_t ret = pa_ret_ok;
    int32_t nret = 0;

    uint8_t com_cmd[13] = {0x3A, '1', '1', '2', '1', '0', '0', '3', 'e', '0', '1', 0x0D, 0x0A};       

    set_mod_addr(com_cmd, pa_addr);
    nret = write(com_fd, com_cmd, 13);
	PALogInfo("len is %d , write com_cmd is %s \n", nret, com_cmd);
    if( nret > 0 )
    {
        int32_t rsp_len = 0;
        uint8_t rsp_buf[256] = {0};
        usleep(500000);
        rsp_len = read(com_fd, rsp_buf, 256);
		PALogInfo("read buf is %s , len is %d \n", rsp_buf, rsp_len);
        if( rsp_len > 0 )
        {
            ret = hx_msg_check(rsp_buf, rsp_len, pa_addr);
            
            hx_pa_status read_statu = {0};
            
			char rsp_data[]={0};
			int value = 0;
            memcpy(rsp_data,rsp_buf+9, 2);     
            value = HexStrValue(rsp_data);

            read_statu.tempthreshold = value;            
            status->tempthreshold = read_statu.tempthreshold;

            if( pa_ret_ok != ret )
            {
                return ret;
            }
        }
        else
        {
            PALogError("nothing read");
            return pa_uart_read_zero;
        }
    }
    else
    {
        PALogError("error: can't write com port, ret=%d, errno=%d", nret, errno);
        return pa_err_uart_write;
    }
    return ret;

}

pa_ret_t hx_set_pa_temp_threshold(int32_t com_fd, mod_addr_t *pa_addr, int8_t temp)
{
    pa_ret_t ret = pa_ret_ok;

    int32_t nret = 0;

    uint8_t com_cmd[15] = {0x3A, '1', '1', '2', '3', '0', '0', '3', 'e', '0', '1', '7', 'f', 0x0D, 0x0A};  

    set_mod_addr(com_cmd, pa_addr);

    int data1, data2;
    data1 = AscToHex(((temp&0xF0)/16)); 
    data2 = AscToHex(temp&0x0F);
     
    com_cmd[11] = data1;
    com_cmd[12] = data2;
    nret = write(com_fd, com_cmd, 15);
	PALogInfo("len is %d , write com_cmd is %s \n", nret, com_cmd);
    if( nret > 0 )
    {
        int32_t rsp_len = 0;
        uint8_t rsp_buf[256] = {0};
        usleep(500000);
        rsp_len = read(com_fd, rsp_buf, 256);
		PALogInfo("read buf is %s , len is %d \n", rsp_buf, rsp_len);
        if( rsp_len > 0 )
        {
            ret = hx_msg_check(rsp_buf, rsp_len, pa_addr);
            if( pa_ret_ok != ret )
            {
                return ret;
            }
 
        }
        else
        {
            PALogError("nothing read");
            return pa_uart_read_zero;
        }

    }
    else
    {
        PALogError("error: can't write com port, ret=%d, errno=%d", nret, errno);
        return pa_err_uart_write;
    }
    return ret;
}

pa_ret_t hx_get_pa_power_threshold(int32_t com_fd, mod_addr_t *pa_addr, hx_pa_status *status)
{
    pa_ret_t ret = pa_ret_ok;
    int32_t nret = 0;

    uint8_t com_cmd[13] = {0x3A, '1', '1', '2', '1', '0', '0', '5', '4', '0', '1', 0x0D, 0x0A};       

    set_mod_addr(com_cmd, pa_addr);
    nret = write(com_fd, com_cmd, 13);
	PALogInfo("len is %d , write com_cmd is %s \n", nret, com_cmd);
    if( nret > 0 )
    {
        int32_t rsp_len = 0;
        uint8_t rsp_buf[256] = {0};
        usleep(500000);
        rsp_len = read(com_fd, rsp_buf, 256);
		PALogInfo("read buf is %s , len is %d \n", rsp_buf, rsp_len);
        if( rsp_len > 0 )
        {
            ret = hx_msg_check(rsp_buf, rsp_len, pa_addr);
            
            hx_pa_status read_statu = {0};
            
			char rsp_data[]={0};
			int value = 0;
            memcpy(rsp_data,rsp_buf+9, 2);     
            value = HexStrValue(rsp_data);

            read_statu.powerthreshold = value;            
            status->powerthreshold = read_statu.powerthreshold;

            if( pa_ret_ok != ret )
            {
                return ret;
            }
        }
        else
        {
            PALogError("nothing read");
            return pa_uart_read_zero;
        }
    }
    else
    {
        PALogError("error: can't write com port, ret=%d, errno=%d", nret, errno);
        return pa_err_uart_write;
    }
    return ret;

}

pa_ret_t hx_set_pa_power_threshold(int32_t com_fd, mod_addr_t *pa_addr, int8_t power)
{
    pa_ret_t ret = pa_ret_ok;

    int32_t nret = 0;

    uint8_t com_cmd[15] = {0x3A, '1', '1', '2', '3', '0', '0', '5', '4', '0', '1', '7', 'f', 0x0D, 0x0A};  

    set_mod_addr(com_cmd, pa_addr);

    int data1, data2;
    data1 = AscToHex(((power&0xF0)/16)); 
    data2 = AscToHex(power&0x0F);
     
    com_cmd[11] = data1;
    com_cmd[12] = data2;
    nret = write(com_fd, com_cmd, 15);
	PALogInfo("len is %d , write com_cmd is %s \n", nret, com_cmd);
    if( nret > 0 )
    {
        int32_t rsp_len = 0;
        uint8_t rsp_buf[256] = {0};
        usleep(500000);
        rsp_len = read(com_fd, rsp_buf, 256);
		PALogInfo("read buf is %s , len is %d \n", rsp_buf, rsp_len);
        if( rsp_len > 0 )
        {
            ret = hx_msg_check(rsp_buf, rsp_len, pa_addr);
            if( pa_ret_ok != ret )
            {
                return ret;
            }
 
        }
        else
        {
            PALogError("nothing read");
            return pa_uart_read_zero;
        }

    }
    else
    {
        PALogError("error: can't write com port, ret=%d, errno=%d", nret, errno);
        return pa_err_uart_write;
    }
    return ret;
}

